// AWTChoicePeer.cpp : implementation file
//

#include "stdafx.h"
#include "awt.h"
#include "AWTChoicePeer.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

IMPLEMENT_DYNAMIC(AWTChoicePeer, CComboBox)

/////////////////////////////////////////////////////////////////////////////
// AWTChoicePeer

AWTChoicePeer::AWTChoicePeer(AWTComponent* comp)
{
	m_comp = comp;
	m_AWTWindow = NULL;
}

AWTChoicePeer::~AWTChoicePeer()
{
	if (m_hWnd != NULL)
		DestroyWindow();
}


BEGIN_MESSAGE_MAP(AWTChoicePeer, CComboBox)
	//{{AFX_MSG_MAP(AWTChoicePeer)
	ON_WM_KEYDOWN()
	ON_WM_CHAR()
	ON_WM_KILLFOCUS()
	ON_WM_SETFOCUS()
	ON_WM_KEYUP()
	ON_WM_CREATE()
	ON_WM_DESTROY()
	ON_CONTROL_REFLECT(CBN_SELCHANGE, OnSelchange)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

void AWTChoicePeer::addItem(CString& item, int index) 
{
	InsertString(index, item);
}

void AWTChoicePeer::clear() 
{
	ResetContent();
}

void AWTChoicePeer::select(int index) 
{
	SetCurSel(index);
}

void AWTChoicePeer::delItems(int start, int end)
{
	for (int i = end; i >= start; i--) 
	{
	    DeleteString(i);
	}
}

int AWTChoicePeer::getSelectedIndex()
{
	int n = GetCurSel();
	if (n == LB_ERR)
		return -1;
	else
		return n;
}

/////////////////////////////////////////////////////////////////////////////
// AWTChoicePeer message handlers
void AWTChoicePeer::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	time_t t=0;
#ifndef UNDER_CE
	time(&t);
#endif
	AWTKeyEvent e(m_comp, AWTKeyEvent::KEY_PRESSED, (long)t, nFlags, nChar, nChar);
	m_comp->processKeyEvent(&e);
	if (m_AWTWindow != NULL)
		m_AWTWindow->SendMessage(WM_AWTKEYEVENT, 0, (LPARAM)&e);

	CComboBox::OnKeyDown(nChar, nRepCnt, nFlags);
}

void AWTChoicePeer::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	time_t t=0;
#ifndef UNDER_CE
	time(&t);
#endif
	AWTKeyEvent e(m_comp, AWTKeyEvent::KEY_TYPED, (long)t, nFlags, nChar, nChar);
	m_comp->processKeyEvent(&e);
	if (nChar == VK_TAB)
		return;
	if (nChar == VK_RETURN)
		return;
	CComboBox::OnChar(nChar, nRepCnt, nFlags);
}

void AWTChoicePeer::OnKillFocus(CWnd* pNewWnd) 
{
	CComboBox::OnKillFocus(pNewWnd);
	
	AWTFocusEvent e(m_comp, AWTFocusEvent::FOCUS_LOST);
	m_comp->processFocusEvent(&e);
	if (m_AWTWindow != NULL)
		m_AWTWindow->SendMessage(WM_AWTFOCUSEVENT, 0, (LPARAM)&e);
}

void AWTChoicePeer::OnSetFocus(CWnd* pOldWnd) 
{
	CComboBox::OnSetFocus(pOldWnd);

	AWTFocusEvent e(m_comp, AWTFocusEvent::FOCUS_GAINED);
	m_comp->processFocusEvent(&e);
	if (m_AWTWindow != NULL)
		m_AWTWindow->SendMessage(WM_AWTFOCUSEVENT, 0, (LPARAM)&e);
}

void AWTChoicePeer::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	time_t t=0;
#ifndef UNDER_CE
	time(&t);
#endif
	AWTKeyEvent e(m_comp, AWTKeyEvent::KEY_RELEASED, (long)t, nFlags, 0, nChar);
	m_comp->processKeyEvent(&e);
	CComboBox::OnKeyUp(nChar, nRepCnt, nFlags);
}

int AWTChoicePeer::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CComboBox::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	m_AWTWindow = getAWTWindow(this);
/*
	CWnd* wnd = this;
	CString str;
	BOOL bFound = FALSE;
	do {
		wnd = wnd->GetParent();
		wnd->GetWindowText(str);
		if (str == "!~AWTWindow~!")
		{
			bFound = TRUE;
			break;
		}
		else if (wnd->IsKindOf(RUNTIME_CLASS(AWTDialogPeer)))
		{
			bFound = TRUE;
			break;
		}
		else if (wnd->IsKindOf(RUNTIME_CLASS(AWTMdiChildWnd)))
		{
			bFound = TRUE;
			break;
		}
	} while (wnd != NULL);
	if (bFound)
		m_AWTWindow = wnd;		
*/
	return 0;
}

void AWTChoicePeer::OnDestroy() 
{
	CComboBox::OnDestroy();
	AWTWindowEvent e(m_comp, AWTWindowEvent::WINDOW_CLOSED);
//	m_comp->processWindowEvent(&e);
	if (m_AWTWindow != NULL)
		m_AWTWindow->SendMessage(WM_AWTWINDOWEVENT, 0, (LPARAM)&e);
}

void AWTChoicePeer::OnSelchange() 
{
	AWTChoice* c = (AWTChoice*)m_comp;
	c->select(GetCurSel());

	int sel = GetCurSel();
	CString str;
	GetLBText(sel, str);

	AWTItemEvent ev(c, sel, (DWORD)&str, AWTItemEvent::SELECTED);
	c->processItemEvent(&ev);
}
